package com.itheima.反射.method.demo1;

import java.lang.reflect.Method;


public class Demo {
    public static void main(String[] args) throws Exception {
        //获取class字节码文件对象
        Class<?> aClass = Class.forName("com.itheima.反射.method.demo1.Student");
        //创建student对象
        Student stu = (Student) aClass.newInstance();
        //method1(aClass);


        //获取method对象
        Method method = aClass.getDeclaredMethod("function4", String.class);
        //暴力反射
        method.setAccessible(true);
        //运行方法
        Object result = method.invoke(stu, "zhangsan");
        //打印结果
        System.out.println(result);
    }

    private static void method1(Class<?> aClass) {
        //获取method中所有public对象(包含继承的方法)数组
        Method[] methods = aClass.getMethods();
        for (Method method : methods) {
            System.out.println(method);
        }

        //获取method中所有对象(不含继承的方法)数组
        Method[] declaredMethods = aClass.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            System.out.println(declaredMethod);
        }
    }
}
